package com.copd.util;

import com.copd.dto.LogCreateRequest;
import com.copd.service.DoctorOperationLogService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 操作日志帮助工具类，用于统一记录患者相关表的操作
 */
@Component
public class OperationLogHelper {

    private static final Logger logger = LoggerFactory.getLogger(OperationLogHelper.class);
    private static final ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 创建包含所有患者相关表的日志请求
     * 
     * @param doctorId 医生ID
     * @param patientId 患者ID
     * @param operationType 操作类型（新增、修改、删除、查询）
     * @param ipAddress IP地址
     * @param deviceInfo 设备信息
     * @param patientInfoDetail 患者基本信息详情（JSON字符串）
     * @param diagnosisDetail 诊断记录详情（JSON字符串，可为null）
     * @param examDetail 检查项目详情（JSON字符串，可为null）
     * @param treatmentDetail 治疗方案详情（JSON字符串，可为null）
     * @param dischargeDetail 出院建议详情（JSON字符串，可为null）
     * @return 完整的日志请求对象
     */
    public static LogCreateRequest createFullPatientLogRequest(
            Integer doctorId,
            Integer patientId,
            String operationType,
            String ipAddress,
            String deviceInfo,
            String patientInfoDetail,
            String diagnosisDetail,
            String examDetail,
            String treatmentDetail,
            String dischargeDetail) {
        
        LogCreateRequest request = new LogCreateRequest();
        request.setDoctorId(doctorId);
        request.setPatientId(patientId);
        request.setOperationType(operationType);
        request.setIpAddress(ipAddress);
        request.setDeviceInfo(deviceInfo);
        
        List<LogCreateRequest.OperationDetailItem> detailItems = new ArrayList<>();
        
        // 添加患者基本信息详情
        if (patientInfoDetail != null) {
            LogCreateRequest.OperationDetailItem patientItem = new LogCreateRequest.OperationDetailItem();
            patientItem.setTableName("patient_info");
            patientItem.setRecordId(patientId);
            patientItem.setOperationDetail(patientInfoDetail);
            detailItems.add(patientItem);
        }
        
        // 添加诊断记录详情
        if (diagnosisDetail != null) {
            LogCreateRequest.OperationDetailItem diagnosisItem = new LogCreateRequest.OperationDetailItem();
            diagnosisItem.setTableName("diagnosis_records");
            diagnosisItem.setRecordId(patientId);
            diagnosisItem.setOperationDetail(diagnosisDetail);
            detailItems.add(diagnosisItem);
        }
        
        // 添加检查项目详情
        if (examDetail != null) {
            LogCreateRequest.OperationDetailItem examItem = new LogCreateRequest.OperationDetailItem();
            examItem.setTableName("examination_items");
            examItem.setRecordId(patientId);
            examItem.setOperationDetail(examDetail);
            detailItems.add(examItem);
        }
        
        // 添加治疗方案详情
        if (treatmentDetail != null) {
            LogCreateRequest.OperationDetailItem treatmentItem = new LogCreateRequest.OperationDetailItem();
            treatmentItem.setTableName("treatment_plans");
            treatmentItem.setRecordId(patientId);
            treatmentItem.setOperationDetail(treatmentDetail);
            detailItems.add(treatmentItem);
        }
        
        // 添加出院建议详情
        if (dischargeDetail != null) {
            LogCreateRequest.OperationDetailItem dischargeItem = new LogCreateRequest.OperationDetailItem();
            dischargeItem.setTableName("discharge_records");
            dischargeItem.setRecordId(patientId);
            dischargeItem.setOperationDetail(dischargeDetail);
            detailItems.add(dischargeItem);
        }
        
        request.setDetailItems(detailItems);
        return request;
    }
    
    /**
     * 记录完整的患者操作日志（包含所有相关表）
     */
    public static void logFullPatientOperation(
            DoctorOperationLogService logService,
            Integer doctorId,
            Integer patientId,
            String operationType,
            String ipAddress,
            String deviceInfo,
            Object patientInfoData,
            Object diagnosisData,
            Object examData,
            Object treatmentData,
            Object dischargeData) {
        
        try {
            // 将对象转换为JSON字符串
            String patientInfoDetail = patientInfoData != null ? objectMapper.writeValueAsString(patientInfoData) : null;
            String diagnosisDetail = diagnosisData != null ? objectMapper.writeValueAsString(diagnosisData) : null;
            String examDetail = examData != null ? objectMapper.writeValueAsString(examData) : null;
            String treatmentDetail = treatmentData != null ? objectMapper.writeValueAsString(treatmentData) : null;
            String dischargeDetail = dischargeData != null ? objectMapper.writeValueAsString(dischargeData) : null;
            
            // 创建完整的日志请求
            LogCreateRequest request = createFullPatientLogRequest(
                doctorId,
                patientId,
                operationType,
                ipAddress,
                deviceInfo,
                patientInfoDetail,
                diagnosisDetail,
                examDetail,
                treatmentDetail,
                dischargeDetail
            );
            
            // 记录日志
            logService.createLog(request);
        } catch (Exception e) {
            logger.error("记录完整患者操作日志失败", e);
        }
    }
} 